package fair.controllerMobile;

import fair.constant.MessageConstant;
import fair.constant.RedisMessageConstant;
import fair.entity.Result;
import fair.utils.TXSMSUtils;
import fair.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * @author fair_wu
 * @version v1.0
 * @package fair.controllerMobile
 * @date 2021-01-28 下午 18:25
 */

@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    JedisPool jedisPool;

    @RequestMapping("/send4Order")
    private Result send4Order(String telephone){
        String validateCode = ValidateCodeUtils.generateValidateCode(4).toString();
        try {
            TXSMSUtils.sendShortMessage(telephone,TXSMSUtils.VALIDATE_CODE, validateCode);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 5 * 60, validateCode);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }

    @RequestMapping("/send4Login")
    private Result send4Login(String telephone){
        String validateCode = ValidateCodeUtils.generateValidateCode(6).toString();
        try {
            TXSMSUtils.sendShortMessage(telephone,TXSMSUtils.VALIDATE_CODE, validateCode);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_LOGIN, 5 * 60, validateCode);
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
